﻿#include "appmonitorlogwidget.h"
#include "ui_appmonitorlogwidget.h"
#include <QMessageBox>

AppMonitorLogWidget::AppMonitorLogWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AppMonitorLogWidget)
{
    ui->setupUi(this);
    reloadWithQueryConditions();

    initMonitorNameCombox();
    initLogStateCombox();

    connect(ui->qureButton, SIGNAL(clicked(bool)), this, SLOT(reloadWithQueryConditions()));
    connect(ui->firtPageButton, SIGNAL(clicked(bool)), this, SLOT(onFirstPageClicked()));
    connect(ui->endPageButton, SIGNAL(clicked(bool)), this, SLOT(onLastPageClicked()));
    connect(ui->prePageButton, SIGNAL(clicked(bool)), this, SLOT(onPrePageClicked()));
    connect(ui->nextPageButton, SIGNAL(clicked(bool)), this, SLOT(onNextPageClicked()));
    connect(ui->timeCombox, SIGNAL(currentIndexChanged(int)), this, SLOT(onQueryTimeChanged(int)));
    connect(ui->deleLogButton, SIGNAL(clicked(bool)), this, SLOT(onDeleteLogButtonCliecked()));

    int i = 0;
    ui->logsTable->setColumnWidth(i++, 220);
    ui->logsTable->setColumnWidth(i++, 220);
    ui->logsTable->setColumnWidth(i++, 220);
    ui->logsTable->setColumnWidth(i++, 220);
    ui->logsTable->setColumnWidth(i++, 220);


}

AppMonitorLogWidget::~AppMonitorLogWidget()
{
    delete ui;
}

void AppMonitorLogWidget::initMonitorNameCombox()
{
    list<QString> names;
    if(f_success == gDBService.getMonitorNames(names))
    {
        int i = 1;

        foreach (QString name, names) {
            ui->appNameCombox->insertItem(i, name);
            i++;
        }
    }
    else
    {
       QMessageBox::warning(this, "提示", "数据库错误,加载应用名称作为查询条件失败");
    }

}

void AppMonitorLogWidget::initLogStateCombox()
{
    ui->logStateCombox->insertItem(1, "成功");
    ui->logStateCombox->insertItem(2, "失败");
}


void AppMonitorLogWidget::reloadWithQueryConditions()
{
    currentPageNum = 1;
    SafeClearListAndDelEle(AppMonitorLog, logs);


    QString* queryMonitorName = NULL;
    QString* startTime = NULL;
    QString* endTime = NULL;
    QString* logState = NULL;


    getQueryConditionsFromUI(queryMonitorName, startTime, endTime, logState);

    int logsCount;
    if(f_success == gDBService.loadMonitorLogsCount(logsCount, queryMonitorName, startTime, endTime, logState))
    {
        int limit = -1;
        if(logsCount > MAX_DISPLAYED_LOGS_COUNT)
        {
            //limit = MAX_DISPLAYED_LOGS_COUNT;
            //QMessageBox::warning(this, "提示", "查询结果日志条数太多,只显示top1000");
        }

        if(f_success == gDBService.loadMonitorLogs(logs, queryMonitorName, startTime, endTime, logState, limit))
        {

            //will display logs
        }
        else
        {
            LOG_ERROR("try to get monitor logs  failed");
            QMessageBox::warning(this, "提示", "读取日志条数失败,数据库异常");
        }
    }
    else
    {
        LOG_ERROR("try to get monitor logs count failed");
        QMessageBox::warning(this, "提示", "读取日志条数失败,数据库异常");
    }

    SafeDeleteObj(queryMonitorName);
    SafeDeleteObj(startTime);
    SafeDeleteObj(endTime);

    displayLogs();
}

void AppMonitorLogWidget::onPrePageClicked()
{

    currentPageNum -= 1;


    displayLogs();
}

void AppMonitorLogWidget::onNextPageClicked()
{
    currentPageNum += 1;

    displayLogs();
}

void AppMonitorLogWidget::onFirstPageClicked()
{
    currentPageNum = 1;
    displayLogs();
}

void AppMonitorLogWidget::onLastPageClicked()
{
    currentPageNum = logs.size()/pageSize + 1;
    displayLogs();
}

void AppMonitorLogWidget::onQueryTimeChanged(int index)
{
    if(index == TIME_QUERY_MODE_LAST_SELECT)
    {
        ui->beginTimeEdit->setEnabled(true);
        ui->endTimeEdit->setEnabled(true);
    }
    else
    {
        ui->beginTimeEdit->setEnabled(false);
        ui->endTimeEdit->setEnabled(false);
    }
}

void AppMonitorLogWidget::onDeleteLogButtonCliecked()
{

    if(QMessageBox::Yes == QMessageBox::information(this, "提示", "确认根据条件删除日志吗?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes))
    {
        QString* queryMonitorName = NULL;
        QString* startTime = NULL;
        QString* endTime = NULL;
        QString* logState = NULL;


        getQueryConditionsFromUI(queryMonitorName, startTime, endTime, logState);

        if(f_success == gDBService.deleteMonitorLogs(queryMonitorName, startTime, endTime, logState))
        {
            QMessageBox::information(this, "提示", "删除日志成功");
            reloadWithQueryConditions();
        }
        else
        {
            QMessageBox::warning(this, "警告", "删除日志失败,数据库错误");
        }
    }

}

void AppMonitorLogWidget::getQueryConditionsFromUI(QString *&queryMonitorName, QString *&startTime, QString *&endTime, QString *&logState)
{

    int timeQueryMode = ui->timeCombox->currentIndex();
    int  monitorNameIndex = ui->appNameCombox->currentIndex();
    int logStateMode = ui->logStateCombox->currentIndex();
    QString monitorName = ui->appNameCombox->currentText();



    SafeDeleteObj(queryMonitorName);
    SafeDeleteObj(startTime);
    SafeDeleteObj(endTime);
    SafeDeleteObj(logState);

    if(logStateMode == LOG_STATE_QUERY_MODE_UNLIMIT)
    {
        logState = NULL;
    }
    else if(logStateMode == LOG_STATE_QUERY_MODE_SUCCESSFUL)
    {
        logState = new QString("1");
    }
    else
    {
        logState = new QString("0");
    }

    if(timeQueryMode == TIME_QUERY_MODE_UNLIMIT)
    {
        startTime = NULL;
        endTime = NULL;
    }
    else if(timeQueryMode == TIME_QUERY_MODE_TODAY)
    {
        QDateTime currentDateTime = QDateTime::currentDateTime();
        currentDateTime.setTime(ZERO_TIME);
        startTime = new QString(sayTime(currentDateTime));
    }
    else if(timeQueryMode == TIME_QUERY_MODE_LAST_3DAYS)
    {
        QDateTime currentDateTime = QDateTime::currentDateTime();
        currentDateTime.setTime(ZERO_TIME);
        currentDateTime = currentDateTime.addDays(-2);
        startTime = new QString(sayTime(currentDateTime));
    }
    else if(timeQueryMode == TIME_QUERY_MODE_LAST_7DAYS)
    {
        QDateTime currentDateTime = QDateTime::currentDateTime();
        currentDateTime.setTime(ZERO_TIME);
        currentDateTime = currentDateTime.addDays(-7);
        startTime = new QString(sayTime(currentDateTime));
    }
    else if(timeQueryMode == TIME_QUERY_MODE_LAST_30DAYS)
    {
        QDateTime currentDateTime = QDateTime::currentDateTime();
        currentDateTime.setTime(ZERO_TIME);
        currentDateTime = currentDateTime.addDays(-30);
        startTime = new QString(sayTime(currentDateTime));
    }
    else if(timeQueryMode == TIME_QUERY_MODE_LAST_SELECT)
    {
        QDateTime startDateTime = ui->beginTimeEdit->dateTime();
        QDateTime endDateTime = ui->endTimeEdit->dateTime();
        if(startDateTime > endDateTime)
        {
            QMessageBox::warning(this, "提示", "结束时间必须大于等于开始时间");
        }
        else
        {
            startTime = new QString(sayTime(startDateTime));
            endTime = new QString(sayTime(endDateTime));
        }
    }

    if(monitorNameIndex != 0)
    {
        queryMonitorName = new QString(monitorName);
    }
}

void AppMonitorLogWidget::displayLogs()
{

    ui->logsTable->clearContents();

    int totalPageNum = logs.size() / pageSize + 1;
    ui->totalFacePageLable->setText("of " + int2QStr(totalPageNum));

    if(currentPageNum <= 0)
    {
        currentPageNum = 1;
    }
    else if (currentPageNum > totalPageNum)
    {
        currentPageNum = totalPageNum;
    }

    ui->pageNumLineEdit->setText(int2QStr(currentPageNum));
    ui->prePageButton->setEnabled(currentPageNum > 1);
    ui->nextPageButton->setEnabled(currentPageNum < totalPageNum);

    int i = 0;
    list<AppMonitorLog *>::iterator iter = logs.begin();
    list<AppMonitorLog *> playedLogs;
    for ( iter;i < currentPageNum * pageSize && iter != logs.end(); iter ++)
    {
        if(i >= (currentPageNum - 1) * pageSize)
        {
            playedLogs.push_back(*iter);
        }
        i++;
    }

    LOG_INFO("do display logs with count:%d, currentPageNum:%d", playedLogs.size(), currentPageNum);

    ui->logsTable->setRowCount(playedLogs.size());

    int y = 0;
    foreach (AppMonitorLog * log, playedLogs) {
        ui->logsTable->setItem(y,0,  new QTableWidgetItem(log->appName));
        ui->logsTable->setItem(y,1,  new QTableWidgetItem(log->action));
        ui->logsTable->setItem(y,2,  new QTableWidgetItem(log->time));
        ui->logsTable->setItem(y,3,  new QTableWidgetItem(intToSuccessOrFail(log->isSuccess)));
        ui->logsTable->setItem(y,4,  new QTableWidgetItem(log->failReason));
        y++;
    }


}

